package cn.kgc.newdb.controller;

import cn.kgc.newdb.pojo.Category;
import cn.kgc.newdb.pojo.Detail;
import cn.kgc.newdb.pojo.ResultEntity;
import cn.kgc.newdb.service.DetailService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class DetailController {
    @Resource
    DetailService detailService;
    @RequestMapping("/selectDetail")
    public ResultEntity<PageInfo<Detail>> select(@RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
                                                 @RequestParam(value = "pageSize",required = false,defaultValue = "3")Integer pageSize,
                                                 @RequestParam(value = "categoryId",required = false,defaultValue = "0")Integer categoryId,
                                                 @RequestParam(value = "title",required = false,defaultValue = "")String title){
        PageInfo<Detail> pageInfo=detailService.select(pageNum,pageSize,categoryId,title);
        return ResultEntity.successWithoutData(pageInfo);
    }
    @RequestMapping("/saveDetail")
    public ResultEntity<String> add(Detail detail){
        try {
            Integer integer=0;
            if(detail.getId()==null){
                integer=detailService.add(detail);
            }else {
                integer=detailService.update(detail);
            }
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
           return ResultEntity.failed(ex.getMessage());
        }
    }
    @RequestMapping("/getDetailById")
    public ResultEntity<Detail> getDetailById(Integer id){
        Detail detail=detailService.selectById(id);
        return ResultEntity.successWithoutData(detail);
    }
    @RequestMapping("/delDetailById")
    public ResultEntity<String> delDetailById(Integer id){
        try{
Integer integer=detailService.delete(id);
return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
       return ResultEntity.failed(ex.getMessage());
        }
    }
}
